package br.com.lh.ura.database;

import java.util.Calendar;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import br.com.lh.ura.adapter.UraAdapter;
import br.com.locaweb.pabx.core.dao.DAO;
import br.com.locaweb.pabx.core.dao.DAOFactory;
import br.com.locaweb.pabx.core.managers.CompanyManager;
import br.com.locaweb.pabx.core.models.Company;
import br.com.locaweb.pabx.core.models.Extension_;
import br.com.locaweb.pabx.core.models.UraComponent;
import br.com.locaweb.pabx.core.models.WuraExtension;

public class UraTransaction {

    private DAOFactory daoFactory;
    private CompanyManager companyManager;

    public UraTransaction() {

    }

    public DAOFactory getDaoFactory() {
	return daoFactory;
    }

    public void setDaoFactory(DAOFactory daoFactory) {
	this.daoFactory = daoFactory;
    }

    public void setCompanyManager(CompanyManager companyManager) {
	this.companyManager = companyManager;
    }

    @Transactional
    public String saveUra(UraXml uraExtension, Integer string) throws Exception {
	WuraExtension wuraExtension = null;
	DAO<WuraExtension> wuraDAO = daoFactory.createDAO(WuraExtension.class);
	if (uraExtension.uraId.equals("-1")) {
	    wuraExtension = new WuraExtension();
	    Company company = companyManager.get(string);
	    wuraExtension.setCompany(company);
	} else {
	    wuraExtension = wuraDAO.get(Integer.valueOf(uraExtension.uraId));
	    DAO<UraComponent> uraComponentDAO = daoFactory.createDAO(UraComponent.class);
	    for (UraComponent component : wuraExtension.getUraComponentList()) {
		uraComponentDAO.delete(component);
	    }
	}
	populateUraValues(uraExtension, wuraExtension);
	try {
	    wuraDAO.save(wuraExtension);
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return uraExtension.uraId;
    }

    private void populateUraValues(UraXml uraExtension, WuraExtension wuraExtension) {
	wuraExtension.setAlias(uraExtension.alias);
	wuraExtension.setDefinition(uraExtension.xmlString);
	wuraExtension.setUpdateTime(Calendar.getInstance());
	if (uraExtension.defaultExtensionId == null) {
	    wuraExtension.setDefaultExtension(null);
	} else {
	    wuraExtension.setDefaultExtension(getExtension(Integer
		    .valueOf(uraExtension.defaultExtensionId)));
	}
	populateUraComponentList(wuraExtension, uraExtension.uraViewList);
	wuraExtension.setNumber(uraExtension.uraExtension);
    }

    private void populateUraComponentList(WuraExtension wuraExtension,
	    List<UraComponent> uraComponentList) {
	for (UraComponent component : uraComponentList) {
	    component.setId(null);
	    component.setWuraExtension(wuraExtension);
	}
	wuraExtension.setUraComponentList(uraComponentList);
    }

    private Extension_ getExtension(int defaultExtensionId) {
	return daoFactory.createDAO(Extension_.class).get(defaultExtensionId);
    }

    private Boolean checkRequestValid(WuraExtension wura, Integer integer) {
	return wura.getCompany().getId().equals(integer);
    }

    @Transactional
    public UraXml loadUra(Integer uraId, Integer integer) {
	WuraExtension wura = daoFactory.createDAO(WuraExtension.class).get(uraId);
	if (wura == null || !checkRequestValid(wura, integer)) {
	    return null;
	}

	try {
	    return UraAdapter.getUraXml(wura);
	} catch (CloneNotSupportedException e) {
	    return null;
	}
    }

    @Transactional
    public UraResource loadUraResources(Integer companyId) {
	Company company = daoFactory.createDAO(Company.class).get(companyId);
	UraResource uraResource = new UraResource();
	uraResource.availableExtension = UraAdapter.getFlexAvailableExtension(company
		.getExtensions_());
	uraResource.soundFileList = UraAdapter.getFlexSoundList(company.getSoundLists(),company.getName());
	return uraResource;

    }

}
